package cn.snmp.mutual.handler;

import java.util.HashMap;
import java.util.LinkedList;

import cn.snmp.mutual.constants.NnTempLetError;
import cn.snmp.mutual.filter.AbstractFilter;
import cn.snmp.mutual.nms.ErrorMessages;
import cn.snmp.mutual.nms.SnmpVarBind;
import lombok.extern.slf4j.Slf4j;
import cn.snmp.mutual.data.ObjService;
import cn.snmp.mutual.common.snmp.SnmpNioTarget;
import cn.snmp.mutual.util.CheckUtilities;
import cn.snmp.mutual.util.OperatorUtilities;
import cn.snmp.mutual.util.Utilities;
/**
 * @author dengkn
 * 获取一页数据GetTablePage
 * */
@Slf4j
public class SnmpGetTablePage {

	public static void hander(SnmpNioTarget snmpTarget, ObjService tableInfo, ObjService result) {
		try {
			snmpTarget.setSNMPOpType(SnmpNioTarget.SNMP_GET_TABLE_PAGE);
			// 计算需要读取的表项的个数
			LinkedList<ObjService> indexNodeLinkedList = new LinkedList<ObjService>();
			// all the normal node(not index node)
			LinkedList<ObjService> accessNodeLinkedList = new LinkedList<ObjService>();
			int mibNodeCount = result.objectSize("MibNode");
			for (int i = 0; i < mibNodeCount; i++) {
				ObjService mibNode = result.objectAt("MibNode", i);
				String strOID = mibNode.getStringValue("OID");
				if ("".equals(strOID)) {
					continue;
				}
				String access = mibNode.getStringValue("Access");
				if (Utilities.isIndexNode(mibNode)) {
					indexNodeLinkedList.add(mibNode);
				}
				if (!access.equalsIgnoreCase("NOACCESS")) {
					accessNodeLinkedList.add(mibNode);
				}
			}
			if (accessNodeLinkedList.size() == 0) {
				log.info("mutual get table page mib node data handle equals = 0!");
				snmpTarget.setFinishTag(true);
				result.setValue("ErrCode", NnTempLetError.SNMP_NODE_NOT_FOUND);
				result.setValue("isFinish", "true");
				result.setValue("ErrDesc", "SNMP_NODE_NOT_FOUND");
				return;
			}
			// String strBaseInstance
			snmpTarget.setBaseInstance(tableInfo.getStringValue("BaseInstance"));
			// set MaxRow parameter of SnmpTarget
			String strPageSize = tableInfo.getStringValue("RealPageSize");

			if (strPageSize.equals("") || !CheckUtilities.isNumberString(strPageSize)) {
				strPageSize = "25";
			}
			int iPageSize = Integer.parseInt(strPageSize);
			snmpTarget.setMaxNumRows(iPageSize);
			ObjService mibNodeOIDTable = new ObjService();
			ObjService mibNodeNameTable = new ObjService();

			LinkedList<String> nodeOIDLinkedList = new LinkedList<String>();
			for (int i = 0; i < accessNodeLinkedList.size(); i++) {
				ObjService mibNode = (ObjService) accessNodeLinkedList.get(i);

				String nodeOID = mibNode.getStringValue("OID");
				String nodeLogicName = mibNode.getStringValue("Logic");

				mibNodeNameTable.setValue(nodeLogicName, nodeOID);

				String isExists = mibNodeOIDTable.getStringValue(nodeOID);
				if (!isExists.equalsIgnoreCase("true")) {
					mibNodeOIDTable.setValue(nodeOID, "true");
					nodeOIDLinkedList.add(nodeOID);
				}
			}
			AbstractFilter rowFilter = AbstractFilter.getFilter(result);
			if (rowFilter != null) {
				rowFilter.init(tableInfo);
			}
			result.setValue("isFinish", "false");
			String [] nodeBaseOIDs = new String[nodeOIDLinkedList.size()];
			for (int i = 0; i < nodeOIDLinkedList.size(); i++) {
				nodeBaseOIDs[i] = (String) nodeOIDLinkedList.get(i);
			}
			HashMap<String, Integer> bindTable = new HashMap<String, Integer>();
			for (int i = 0; i < nodeBaseOIDs.length; i++) {
				bindTable.put(nodeBaseOIDs[i], new Integer(i));
			}
			snmpTarget.setObjectIDList(nodeBaseOIDs);
			snmpTarget.setSTPageParam(indexNodeLinkedList, accessNodeLinkedList, bindTable, mibNodeNameTable,
					rowFilter);
			if ((nodeBaseOIDs == null) || (nodeBaseOIDs.length < 1)) {
				snmpTarget.setFinishTag(true);
				result.setValue("ErrCode", "21");
				result.setValue("ErrDesc", ErrorMessages.getErrorString(21));
			}
		} catch (Exception e) {
			log.error("mutual get table page mib node data Exception error!", e);
			result.setValue("ErrCode", "801");
			result.setValue("ErrDesc", e.getMessage());
			snmpTarget.setFinishTag(true);
		}
	}

	/**
	 * @param target SnmpNioTarget实例
	 * @return 表结构和值
	 */
	public static void parser(SnmpNioTarget target) {
		try {
			SnmpVarBind[][] svb = (SnmpVarBind[][]) target.getOrgResult();
			OperatorUtilities.getTableRowSets(target.getOsParam(), target.getResult(), svb);
		} catch (Exception e) {
			log.error("mutual get table page mib node data  parser error!", e);
			target.getResult().setValue("ErrCode", "801");
			target.getResult().setValue("ErrDesc", e.getMessage());
		}
	}
}
